#include "stdafx.h"
#include "DeleteAllDataIn.h"

DeleteAllDataIn::DeleteAllDataIn()
{
}

DeleteAllDataIn::~DeleteAllDataIn()
{
}

void DeleteAllDataIn::ChangeFuncToArchi(CString fileName)
{
	PublicFunc::ReadSourceFile(fileName, m_csReadlines);

	if (!IsFileValid2(fileName)) return;

	CFileFind finder;
	BOOL bResult = finder.FindFile(fileName);
	if (bResult)
	{
		DeleteFile(fileName);
	}
	try
	{
		CString str;
		CStdioFile file(fileName, CFile::modeCreate | CFile::modeWrite);			file.SetLength(0);

		int num = this->m_csReadlines.GetSize();
		for (int k = 0; k < num; k++)
		{
			CString string = m_csReadlines[k], string1;
			if (PublicFunc::IsStrHasStr(string, _T("IMPLEMENT_COMMAND")))
			{
				int id = string.Find(_T(")"));
				int len = string.GetLength();
				string1 = string.Mid(0, id-1) + _T("_archi") + string.Mid(id-1, len - id + 2);
			}
			else
			{
				string1 = string;
			}

			file.WriteString(string1);
			if (k == num - 1)
			{
				int kkk = 0;
			}
			else
			{
				file.WriteString("\n");
			}
		}
		file.Close();
	}
	catch (CFileException* e)
	{
		e->ReportError();
		e->Delete();

		return;
	}
}

void DeleteAllDataIn::DeleteFunction(CString fileName, CString csFunction)
{
	PublicFunc::ReadSourceFile(fileName, m_csReadlines);

	int ipostart = -1, ipos = -1;
	if (PublicFunc::FindFunctionHeadTailPos(m_csReadlines, csFunction, ipostart, ipos))
	{
		CFileFind finder;
		BOOL bResult = finder.FindFile(fileName);
		if (bResult)
		{
			DeleteFile(fileName);
		}
		try
		{
			CString str;
			CStdioFile file(fileName, CFile::modeCreate | CFile::modeWrite);			file.SetLength(0);

			int num = this->m_csReadlines.GetSize();
			for (int k = 0; k < num; k++)
			{
				CString string = m_csReadlines[k], string1;
				if (k >= ipostart&&k <= ipos)
				{
					string = _T("//") + string;
				}

				file.WriteString(string);

				if (k != num - 1)
				file.WriteString("\n");
			}
			file.Close();
		}
		catch (CFileException* e)
		{
			e->ReportError();
			e->Delete();

			return;
		}
	}
}

bool DeleteAllDataIn::Do(CString fileName)
{
	ChangeFuncToArchi(fileName);
	//DeleteFunction(fileName, _T("::dataIn(DcDataFiler"));
	//DeleteFunction(fileName, _T("::dataOut(DcDataFiler"));
	return true;
}

bool DeleteAllDataIn::IsFileValid2(CString fileName)
{
	int num = this->m_csReadlines.GetSize();
	for (int k = 0; k < num; k++)
	{
		CString string = m_csReadlines[k], string1;
		if (PublicFunc::IsStrHasStr(string, _T("IMPLEMENT_COMMAND")))
		{
			return true;
		}
	}

	return false;
}
